package com.lida.zwservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lida.commonuntils.R;
import com.lida.zwservice.entity.OneSubjectVo;
import com.lida.zwservice.entity.PersonalDetailedSubject;
import com.lida.zwservice.entity.SubjectVo;
import com.lida.zwservice.service.PersonalDetailedSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 账务类别 前端控制器
 * </p>
 *
 * @author LiDa
 * @since 2022-01-26
 */
@RestController
@RequestMapping("/zwservice/subject")
@CrossOrigin
public class PersonalDetailedSubjectController {

    @Autowired
    private PersonalDetailedSubjectService subject;

    @GetMapping("getSubject/{parentid}")
    public R getSubject(@PathVariable String parentid){
        List<OneSubjectVo> list=subject.selectSubject(parentid);
        return R.success().data("list",list);
    }

    @PostMapping("addSubject/{id}")
    public R addSubject(@PathVariable String id,
                        @RequestBody PersonalDetailedSubject personalDetailedSubject){
        personalDetailedSubject.setParentId(id);
        subject.save(personalDetailedSubject);
        return R.success();
    }

    @GetMapping("getTwoSubject/{id}")
    public R getTwoSubject(@PathVariable String id){
        QueryWrapper<PersonalDetailedSubject> wrapper=new QueryWrapper();
        wrapper.eq("parent_id",id);
        List<PersonalDetailedSubject> list=subject.list(wrapper);

        List<SubjectVo> subjectlist=new ArrayList<>();
        for(int i=0; i<list.size(); i++){
            SubjectVo subject =new SubjectVo();
            PersonalDetailedSubject personalSubject= list.get(i);

            BeanUtils.copyProperties(personalSubject,subject);

            subjectlist.add(subject);

        }
        return R.success().data("subjectlist",subjectlist);
    }

}

